﻿<?php
include('database/db.php');
$mysql=new mysql();
$ordersid = $_REQUEST[ordersid];
if ($ordersid) {
	$ordersids = explode(',',$ordersid);
	$len = count($ordersids);
	$state=1;
	$flag = true;
	for ($i=0; $i < $len; $i++) { 
		// 调用信息推送接口
		$url = "http://120.24.0.10/xzpf/api/jpush/jpush.php?state=".$state."&ordersid=".$ordersids[$i];
		file_get_contents($url);
		// 调用信息推送接口 end
		$sql="update goodsorders set state='".$state."' where ordersid='".$ordersids[$i]."'";
		$mysql->query($sql);
		if ($mysql->affected_rows()>0) {
		// 记录到现金流量表
			// 根据单号查找订单信息
			$sql="select * from goodsorders where ordersid='".$ordersids[$i]."'";
			$goodsorders=$mysql->getRow($sql);
			// 根据单号查找订单信息 end
	        $sql="select name from shop where shopid=".$goodsorders[shopid]."";
	        $shopname=$mysql->getOne($sql);
	        // 记录消费者购买商品时商家的现金流量记录
	        $content=$shopname."获得销售收入".$goodsorders[totalprice]."元";
	        $sql="INSERT INTO cash_flow (amount,shopid,content) value (".$goodsorders[totalprice].",".$goodsorders[shopid].",'".$content."')";
	        $mysql->query($sql);
	        // 记录消费者购买商品时商家的现金流量记录 end
	        // 记录消费者购买商品时滞留在平台的现金流量记录
	        $shopid=0; // shopid=0表示平台
	        $content="消费者购买".$shopname."商品后,".$shopname."滞留在平台的资金,共".$goodsorders[totalprice]."元";
	        $sql="INSERT INTO cash_flow (amount,shopid,fromshopid,content) value (".$goodsorders[totalprice].",".$shopid.",".$goodsorders[shopid].",'".$content."')";
	        $mysql->query($sql);
	        // 记录消费者购买商品时滞留在平台的现金流量记录 end
	    // 记录到现金流量表 end
		// 更新平台的资金
			$sql="UPDATE admin set property=property+".$goodsorders[totalprice];
			$mysql->query($sql);
		// 更新平台的资金 end
		// 更新商家的余额
			$sql="UPDATE shop set balance=balance+".$goodsorders[totalprice]." where shopid=".$goodsorders[shopid]."";
	      	$mysql->query($sql);
		// 更新商家的余额 end
		}else{
			$flag = false;
		}
	}
	if ($flag) {
		$arr = array('code' => 100, 'tip' =>'支付成功');
	}else{
		$arr = array('code' => 300, 'tip' =>'支付失败');
	}
}else{
	$arr = array('code' => 200, 'tip' =>'参数传入有误,需传入参数:订单号=>ordersid');
}
echo json_encode($arr);